package com.gxa.modules.app.service.impl;

import com.gxa.modules.app.mapper.BuyMapper;
import com.gxa.modules.app.service.BuyService;
import com.gxa.modules.sys.dto.PurchaseAddDto;
import com.gxa.modules.sys.entity.User;
import com.gxa.modules.sys.mapper.PurchaseMapper;
import com.gxa.modules.sys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class BuyServiceImpl implements BuyService {

    @Autowired
    private BuyMapper buyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Transactional
    @Override
    public String addBuy(PurchaseAddDto purchaseAddDto) {

        User user = this.userMapper.queryAccount(purchaseAddDto.getUserId());

        System.out.println("userid是------"+purchaseAddDto.getUserId());

        System.out.println("余额是---------"+user.getAccount());
        System.out.println("价格是------------"+purchaseAddDto.getPrice());

        BigDecimal price = purchaseAddDto.getPrice();
        BigDecimal account = user.getAccount();

        if (price.compareTo(account) == 1){

            return "余额不足";
        }
        this.userMapper.updateAccount(purchaseAddDto.getPrice(),purchaseAddDto.getUserId()); //扣款

        purchaseAddDto.setBuyTime(new Date(System.currentTimeMillis()));
        purchaseAddDto.setChannel(1);

        this.purchaseMapper.addpurchase(purchaseAddDto); //添加卡卷

        return "购买成功";
    }

}

